package com.tang4j.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.tang4j.core.model.AbstractModel;
import com.tang4j.core.util.DataUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @author saber
 * @since 2019-11-12
 */
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "ThSysUser", description = "用户表")
public class ThSysUser extends AbstractModel implements UserDetails {

    private static final long serialVersionUID = 1L;
//    public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();

    @TableField(exist = false)
    private List<ThSysRole> thSysRoles;

    @ApiModelProperty(value = "英文的登录id")
    @TableField("english_login_id")
    private String englishLoginId;

    /**
     * 用户登录密码，用户的密码不应该暴露给客户端
     */
    //@JsonIgnore
    @ApiModelProperty(value = "登录密码（加密存储）")
    @TableField("password")
    private String password;


    @ApiModelProperty(value = "姓名")
    @TableField("name")
    private String name;


    @ApiModelProperty(value = "昵称（花名）")
    @TableField("nick_name")
    private String nickName;


    @ApiModelProperty(value = "头像地址(完整的http地址)")
    @TableField("head_img_url")
    private String headImgUrl;


    @ApiModelProperty(value = "排序")
    @TableField("order_no")
    private Integer orderNo;

    @ApiModelProperty(value = "是否启用")
    @TableField("enable")
    protected Integer enable;


    public void setPassword(String password) {
        this.password = password;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        if (thSysRoles != null) {
            thSysRoles.forEach(thSysRole -> {
                if (!DataUtil.isEmpty(thSysRole.getCode())) {
                    authorities.add(new SimpleGrantedAuthority(
                            thSysRole.getCode()));
                }
            });
        }
        return authorities;
    }


    @Override
    public String getUsername() {
        return this.getEnglishLoginId();
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public List<ThSysRole> getThSysRoles() {
        return thSysRoles;
    }

    public void setThSysRoles(List<ThSysRole> thSysRoles) {
        this.thSysRoles = thSysRoles;
    }

    public String getEnglishLoginId() {
        return englishLoginId;
    }

    public void setEnglishLoginId(String englishLoginId) {
        this.englishLoginId = englishLoginId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getHeadImgUrl() {
        return headImgUrl;
    }

    public void setHeadImgUrl(String headImgUrl) {
        this.headImgUrl = headImgUrl;
    }

    public Integer getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(Integer orderNo) {
        this.orderNo = orderNo;
    }

    @Override
    public Integer getEnable() {
        return enable;
    }

    @Override
    public AbstractModel setEnable(Integer enable) {
        this.enable = enable;
        return this;
    }
}
